home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / tex / tex29 / sttexsrc.zoo / etc / index.c next >
Encoding:
C/C++ Source or Header  |  1988-10-16  |  5.3 KB  |  372 lines

  1. /*
  2.  * index.c
  3.  * ++jrb  bammi@dsrgsun.ces.CWRU.edu
  4.  *
  5.  * Usage: index [infile] [outfile]
  6.  *
  7.  */
  8.  
  9. #include <stdio.h>
  10.  
  11. #ifdef DLIBS
  12. #include <string.h>
  13. #endif
  14.  
  15. typedef struct _pagelist {
  16.     int    page;
  17.     struct    _pagelist *next;
  18. } PAGELIST;
  19.  
  20. typedef struct {
  21.     char *name;
  22.     PAGELIST *pagelist;
  23. } ITEM;
  24.  
  25. ITEM **itemlist;
  26. int nitem = 0;
  27. int maxitem = 0;
  28.  
  29. FILE *in, *out, *fopen();
  30. char *rindex(), *malloc(), *realloc(), *strcpy();
  31. char infile[128], outfile[128];
  32.  
  33. #ifdef ATARI
  34. long _stksize = 8L * 1024L;
  35. #endif
  36.  
  37. main(argc, argv)
  38. int argc;
  39. char **argv;
  40. {
  41.     switch(argc)
  42.     {
  43.         case 1:
  44.             strcpy(infile, "STDIN");
  45.             strcpy(outfile, "STDOUT");
  46.             in = stdin;
  47.             out = stdout;
  48.             break;
  49.         
  50.         case 2:
  51.             if((in = fopen(*++argv, "r")) == (FILE *)NULL)
  52.             {
  53.                 perror(*argv);
  54.                 exit(1);
  55.             }
  56.             strcpy(infile, *argv);
  57.             basename(outfile, *argv);
  58.             strcat(outfile,".ind");
  59.             if((out = fopen(outfile, "w")) == (FILE *)NULL)
  60.             {
  61.                 perror(outfile);
  62.                 exit(2);
  63.             }
  64.             break;
  65.  
  66.         case 3:
  67.             if((in = fopen(*++argv, "r")) == (FILE *)NULL)
  68.             {
  69.                 perror(*argv);
  70.                 exit(3);
  71.             }
  72.             strcpy(infile, *argv);
  73.             if((out = fopen(*++argv, "w")) == (FILE *)NULL)
  74.             {
  75.                 perror(*argv);
  76.                 exit(4);
  77.             }
  78.             strcpy(outfile, *argv);
  79.             break;
  80.  
  81.         default:
  82.             fprintf(stderr,"Usage: %s [infile] [outfile]\n",
  83. #ifdef ATARI
  84.             "index");
  85. #else
  86.             argv[0]);
  87. #endif
  88.             exit(5);
  89.         }
  90.  
  91.     readin();
  92.     fclose(in);
  93.     
  94.     printout();
  95.     fclose(out);
  96.     
  97.     exit(0);
  98. }
  99.  
  100. #ifdef ATARI
  101. #define SEP     '\\'
  102. #else
  103. #define SEP    '/'
  104. #endif
  105.  
  106. basename(s, name)
  107. register char *s, *name;
  108. {
  109.     register char *p, *q;
  110.     extern char *rindex();
  111.  
  112.     if((p = rindex(name, SEP)) != (char *)NULL)
  113.         p = rindex(p, '.');
  114.     else
  115.         p = rindex(name, '.');
  116.  
  117.     if(p == (char *)NULL)
  118.     {
  119.         strcpy(s, name);
  120.         return;
  121.     }
  122.  
  123.     for(q = name; q != p; q++, s++)
  124.         *s = *q;
  125.  
  126.     *s = '\0';
  127. }
  128.     
  129. readin()
  130. {
  131.     char line[1024];
  132.     extern char *fgets();
  133.  
  134.     while(fgets(line, 1024, in) != (char *)NULL)
  135.         process(line);
  136. }
  137.  
  138. process(l)
  139. register char *l;
  140. {
  141.     char name[1024];
  142.     char page[16];
  143.     register char *p, *q;
  144.     register int level;
  145.     extern int atoi();
  146.     
  147.     for(p = l; *p != '{'; p++)
  148.     {
  149.         if(*p == '\0')
  150.         {
  151.             fprintf(stderr,"Skipped Illegal Entry %s\n", l);
  152.             return;
  153.         }
  154.     }
  155.     
  156.     for (q = name, level = 0, p++; !((*p == '}') && (level == 0));
  157.          p++, q++)
  158.     {
  159.         *q = *p;
  160.         if(*p == '{')
  161.             level++;
  162.         else if (*p == '}')
  163.             level--;
  164.         else if (*p == '\0')
  165.         {
  166.             fprintf(stderr,"Skipped Illegal Entry %s\n", l);
  167.             return;
  168.         }
  169.     }
  170.  
  171.     *q = '\0';
  172.     
  173.     for(p++; *p != '{'; p++)
  174.     {
  175.         if(*p == '\0')
  176.         {
  177.             fprintf(stderr,"Skipped Illegal Entry %s\n", l);
  178.             return;
  179.         }
  180.     }
  181.     for (q = page, level = 0, p++; !((*p == '}') && (level == 0));
  182.          p++, q++)
  183.     {
  184.         *q = *p;
  185.         if(*p == '{')
  186.             level++;
  187.         else if (*p == '}')
  188.             level--;
  189.         else if (*p == '\0')
  190.         {
  191.             fprintf(stderr,"Skipped Illegal Entry %s\n", l);
  192.             return;
  193.         }
  194.     }
  195.  
  196.     *q = '\0';
  197.  
  198.     enter(name, atoi(page));
  199. }
  200.  
  201. enter(nam, num)
  202. char *nam;
  203. int num;
  204. {
  205.     register int i, r;
  206.     
  207.     for(i = 0; i < nitem; i++)
  208.     {
  209.         if((r = strcmp(itemlist[i]->name, nam)) == 0)
  210.         {
  211.             addpage(i, num);
  212.             return;
  213.         }
  214.         else
  215.         {
  216.             if(r > 0)
  217.             {
  218.                 additem( i, nam, num);
  219.                 return;
  220.             }
  221.         }
  222.     }
  223.     additem(nitem, nam, num);
  224. }
  225.  
  226. additem(pos, nam, num)
  227. int pos;
  228. char *nam;
  229. int num;
  230. {
  231.     register ITEM *item;
  232.     register int i;
  233.     extern char *myalloc();
  234.     extern char *strcpy();
  235.     extern int strlen();
  236.  
  237.     if(nitem >= maxitem)
  238.         expandit();
  239.     if(pos < nitem)
  240.     {
  241.         for(i = nitem; i > pos; i--)
  242.             itemlist[i] = itemlist[i-1];
  243.     }
  244.     
  245.     item = (ITEM *)myalloc(sizeof(ITEM));
  246.     item->name = strcpy(myalloc(strlen(nam) + 1), nam);
  247.     item->pagelist = (PAGELIST *)NULL;
  248.     itemlist[pos] = item;
  249.     addpage(pos, num);
  250.     nitem++;
  251.     
  252. }
  253.  
  254. #define CHUNKSIZE  10
  255.  
  256. expandit()
  257. {
  258.     extern char *myalloc();
  259.     extern char *realloc();
  260.     
  261.  
  262.     if(maxitem == 0)
  263.     {
  264.         itemlist = (ITEM **)myalloc(sizeof(ITEM *) * CHUNKSIZE);
  265.         maxitem = CHUNKSIZE;
  266.         return;
  267.     }
  268.  
  269.     if((itemlist = (ITEM **)realloc(itemlist, sizeof(ITEM *) * (maxitem +
  270.                    CHUNKSIZE))) == (ITEM **)NULL)
  271.     {
  272.         fprintf(stderr,"Not enough memory\n");
  273.         exit(7);
  274.     }
  275.     maxitem += CHUNKSIZE;
  276. }
  277.  
  278. char *myalloc(s)
  279. unsigned int s;
  280. {
  281.     register char *r;
  282.     extern char *malloc();
  283.     
  284.     if((r = malloc(s)) == (char *)NULL)
  285.     {
  286.         fprintf(stderr,"Out of memory\n");
  287.         exit(8);
  288.     }
  289.     return r;
  290. }
  291.  
  292. addpage(i, num)
  293. int i, num;
  294. {
  295.     register PAGELIST *p, *prev;
  296.     register PAGELIST *q;
  297.     
  298.     q = (PAGELIST *)myalloc(sizeof(PAGELIST));
  299.     q->page = num;
  300.     q->next = (PAGELIST *)NULL;
  301.     
  302.     if((p = itemlist[i]->pagelist) == (PAGELIST *)NULL)
  303.     {
  304.         itemlist[i]->pagelist = q;
  305.         return;
  306.     }
  307.     
  308.     for(prev = p; p != (PAGELIST *)NULL; p = p->next)
  309.     {
  310.         if(p->page == num)
  311.         {
  312.             free(q);
  313.             return;
  314.         }
  315.         if(p->page > num)
  316.         {
  317.             q->next = p;
  318.             if(p != itemlist[i]->pagelist)
  319.                 prev->next = q;
  320.             else
  321.                 itemlist[i]->pagelist = q;
  322.             return;
  323.         }
  324.         prev = p;
  325.     }
  326.  
  327.     prev->next = q;
  328. }
  329.  
  330.  
  331.  
  332. printout()
  333. {
  334.     register int i;
  335.     register PAGELIST *p;
  336.     register char fc;
  337.     register char *s;
  338.     
  339.     fprintf(out,"\\begin{theindex}\n");
  340.     
  341.     for(fc = itemlist[0]->name[0], i = 0; i < nitem; i++)
  342.     {
  343.         if(*(s = itemlist[i]->name) != fc)
  344.         {
  345.             fprintf(out, "\\indexspace\n");
  346.             fc = *s;
  347.         }
  348.         
  349.         
  350.         fprintf(out, "\\item %s", s);
  351.         for (p = itemlist[i]->pagelist; p != (PAGELIST *)NULL; p =
  352.              p->next)
  353.         {
  354.             if(p->next == (PAGELIST *)NULL)
  355.                 fprintf(out, " %d", p->page);
  356.             else
  357.                 fprintf(out," %d,", p->page);
  358.         }
  359.         fprintf(out, "\n");
  360.     }
  361.     fprintf(out,"\\end{theindex}\n");
  362.  
  363. }
  364.  
  365. #ifdef DLIBS
  366. perror(s)
  367. char *s;
  368. {
  369.     fprintf(stderr,"Cannot Open %s\n", s);
  370. }
  371. #endif
  372.